package com.yidiandao.business.manager;

import com.yidiandao.business.data.bean.Discount;
import com.yidiandao.business.data.bean.FavourableInfo;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/** 满减活动Manager */
public class ReduceManager {

    private List<FavourableInfo> mReduceFavourableInfoList = new ArrayList<>();

    private ReduceManager() {
    }

    public static ReduceManager getInstance() {
        return ReduceManager.Singleton.instance;
    }

    public void clearAllReduce() {
        mReduceFavourableInfoList.clear();
    }

    public List<FavourableInfo> getAllReduceFavourableInfos() {
        return mReduceFavourableInfoList;
    }

    public void addReduceFavourableInfo(FavourableInfo favourableInfo) {
        // 如果包含活动信息，则先将就的活动信息移除(在修改商品数量重新获取到的活动信息应该作为最新的)
        //        if (mReduceFavourableInfoList.contains(favourableInfo)) {
        //            mReduceFavourableInfoList.remove(favourableInfo);
        //        }
        mReduceFavourableInfoList.add(favourableInfo);
    }

    /** 更新满减活动的信息 */
    public void updateReduceInfo(FavourableInfo reduceInfo) {
        Iterator<FavourableInfo> iterator = mReduceFavourableInfoList.iterator();
        boolean hasInfo = false;
        while (iterator.hasNext()) {
            FavourableInfo info = iterator.next();
            if (info.equals(reduceInfo)) {
                iterator.remove();
                hasInfo = true;
                break;
            }
        }

        if (hasInfo) {
            mReduceFavourableInfoList.add(reduceInfo);
        }
    }

    /** 计算最优的订单减免活动 */
    public void calculateBestOrderReduce() {

        List<FavourableInfo> allReduceFavourableInfos =
                ReduceManager.getInstance().getAllReduceFavourableInfos();

        for (FavourableInfo reduceInfo : allReduceFavourableInfos) {
            String belongType = reduceInfo.getBelongType();
            if (FavourableInfo.BELONG_ORDER.equals(belongType)) {
                // 属于订单满减
                // 重置订单减免的情况，如：是否否满足减免条件
                // 不能在ListItem中设置，因为ListItem有复用问题，里面的选择可能会无效
                Shop belongShop = reduceInfo.getBelongShop();

                List<Discount> discountList = reduceInfo.getDiscountList();

                // 当前店铺所有商品(包括以选择、未选择)的总价满足满赠的条件
                // 就会有 discountList
                // discountList 价格从高到低
                // 遍历discountList，寻找最新的减免活动
                float priceOfShopSelectedGoods =
                        ShoppingCartManager.getInstance()
                                .getTotalPriceOfShopSelectedGoods(belongShop);

                // 寻找满足条件的满减活动
                int bingoIndex = -1;
                for (int i = 0; i < discountList.size(); i++) {
                    Discount discount = discountList.get(i);
                    if (discount.getMinAmount() <= priceOfShopSelectedGoods) {
                        reduceInfo.setBestDiscount(discount);
                        bingoIndex = i;
                        break;
                    }
                }

                // 所有的满减活动都不满足
                if (bingoIndex == -1) {
                    reduceInfo.setBestDiscount(null);
                }
            }
        }
    }

    /** 获取所有减免活动优惠的价格 */
    public float getAllSelectedReduceInfoPrice() {

        float totalPrice = 0;

        for (FavourableInfo reduceInfo : mReduceFavourableInfoList) {
            if (reduceInfo.getBelongType().equals(FavourableInfo.BELONG_GOODS)) {
                // 商品减免活动
                // 并且活动所属商品已经选择
                Goods belongGoods = reduceInfo.getBelongGoods();
                if (belongGoods.isSelected()) {
                    totalPrice += reduceInfo.getDiscountMoney();
                }
            } else {
                // 订单减免活动
                // 直接获取满足条件的满减活动
                Discount bestDiscount = reduceInfo.getBestDiscount();
                if (bestDiscount != null) {
                    totalPrice += bestDiscount.getDiscount();
                }
            }
        }

        return totalPrice;
    }

    private static class Singleton {
        private static final ReduceManager instance = new ReduceManager();
    }
}
